using UnityEngine;
using System.Collections.Generic;

namespace CGF
{
    public enum EArea { Common, China, Brazil, India, Vietnam }
    public enum EResServerType { TestServer, FormalServer }

    [System.Serializable]
    public class PackInfo
    {
        public string tag;
        public EArea releaseArea = EArea.Common;
        public string companyName;
        public string productNameTest;
        public string productName;
        public Texture2D appIcon;
        public string fileName;
        public string packName;
        public string keystorePath;
        public string keystoreAlias;
        public string keystorePasswd;
        public string versionName = "1.0";
        public int versionCode = 1;
        public bool iosStorePack;
        public string firebaseIOSUrlScheme = "";
        //服务器
        public string testServer = "http://127.0.0.1";
        public string formalServer = "http://127.0.0.1";
        public EResServerType currServerType;
        public EResStrategy resStrategy;
        //资源导出目录
        public string resExportDir = "E:/";
        public bool useCommonHotfix = true;
    }

    [System.Serializable]
    public class Folder
    {
        public Object folderObj;
        public string path;
    }

    //RemoteOnly 直接读取远程AB包，减小安装包大小
    //StreamingAssetsOnly 永不联网，直接将AB包放入StreamingAssets目录
    //StreamingAssetsAndRemote 应对审核，首个版本AB资源打入包内，无更新时直接使用包内资源，有更新时自动更新
    //Resources 不打Ab包,直接在Resources内创建预置体
    public enum EResStrategy { RemoteOnly, StreamingAssetsOnly, StreamingAssetsAndRemote, Resources }

    /// <summary>
    /// 框架配置
    /// </summary>
    [CreateAssetMenu(menuName = "CGFConfig")]
    public class CGFConfig : ScriptableObject
    {
        private static CGFConfig _instance;
        public static CGFConfig Instance
        {
            get
            {
                if (!_instance)
                {
                    _instance = Resources.Load<CGFConfig>(typeof(CGFConfig).Name);
                }
                return _instance;
            }
        }

        public PackInfo[] packs = new PackInfo[1];
        public int currPackIndex;
        public bool isAppBundle;

        public static string Platform
        {
            get
            {
#if UNITY_ANDROID
                return "android";
#elif UNITY_IOS
                return "ios";
#elif UNITY_WEBGL
                return "web";
#else
                return "pc";
#endif
            }
        }

        //编辑器是否使用AssetBundle
        [Space]
        public bool _editorUseAB;
        public bool UseAB { get { return Application.isEditor ? _editorUseAB : true; } }

        public string CompanyName { get { return packs[currPackIndex].companyName; } }

        public string ProductName { get { return packs[currPackIndex].productName; } }

        public string ProductNameTest { get { return packs[currPackIndex].productNameTest; } }

        public string FileName { get { return packs[currPackIndex].fileName; } }

        public string PackName { get { return packs[currPackIndex].packName; } }

        public string VersionName { get { return packs[currPackIndex].versionName; } }

        public int VersionCode { get { return packs[currPackIndex].versionCode; } }

        public string VersionFullName
        {
            get
            {
                //AppHelper.GetAppVersionName()由Version Name和“是否商店包标记”拼接
                bool isStorePack = isAppBundle;
                var pack =  packs[currPackIndex];
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                    isStorePack = pack.iosStorePack;
                }
                string versionStr = $"{pack.versionName}.{pack.versionCode} ({(isStorePack ? "store" : "online")})";
                return versionStr;
            }
        }

        public string ResServer
        {
            get
            {
                var pack = packs[currPackIndex];
                if (Application.isEditor)
                {
                    return pack.testServer;
                }
                if (Define.IsWebGL)
                {
                    return JSHelper.GetCurrentDomain();
                }
                return pack.currServerType == EResServerType.TestServer ? pack.testServer : pack.formalServer;
            }
        }

        public EResServerType ResServerType
        {
            get
            {
                if (Application.isEditor)
                {
                    return EResServerType.TestServer;
                }
                if (Define.IsWebGL)
                {
                    string domain = JSHelper.GetCurrentDomain();
                    bool isLocal = domain.Contains("localhost") || domain.Contains("http://127") || domain.Contains("http://192");
                    return isLocal ? EResServerType.TestServer : EResServerType.FormalServer;
                }
                return packs[currPackIndex].currServerType;
            }
        }

        public EResStrategy ResStrategy
        {
            get
            {
                EResStrategy strategy = packs[currPackIndex].resStrategy;
                if (Define.IsWebGL && strategy == EResStrategy.RemoteOnly)
                {
                    strategy = EResStrategy.StreamingAssetsAndRemote;
                    Debug.Log("WebGL平台，RemoteOnly模式自动当成StreamingAssetsAndRemote模式");
                }
                return strategy;
            }
        }

        public EArea ReleaseArea { get { return packs[currPackIndex].releaseArea; } }

        public string Tag { get { return packs[currPackIndex].tag; } }

        public string ResExportDir { get { return GetResExportDir(currPackIndex); } }

        public string GetResExportDir(int packIndex)
        {
            var pack = packs[packIndex];
#if UNITY_EDITOR
            if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
            {
                //iOS直接导到当前项目根目录
                return $"./res/{pack.releaseArea.ToString()}/{pack.tag}/{Platform}"; 
            }
#endif
            return $"{pack.resExportDir.Replace("\\", "/")}/res/{pack.releaseArea.ToString()}/{pack.tag}/{Platform}";
        }

        public List<Folder> extraUpdateFolders = new List<Folder>();
    }
}
